<?php

namespace app\admin\controller\sum;

use addons\sum\library\BaiduTransService;
use app\common\controller\Backend;

/**
 * 测试管理
 *
 * @icon fa fa-circle-o
 */
class BaiduTrans extends Backend
{

    public function dotrans(){
        $servicesConfig = json_decode(\addons\sum\model\Config::get(['name' => 'services'])->value, true);
        $btConfig=$servicesConfig['baidu_trans'];
        $default_lang=$btConfig['default_lang'];
        if($default_lang=="zh_tw"){
            $from='cht';
        }elseif ($default_lang=="en"){
            $from='en';
        }elseif ($default_lang=="zh_cn"){
            $from='zh';
        }
        $lang = $this->request->request("lang");
        if($lang=="zh_tw"){
            $to='cht';
        }elseif ($lang=="en"){
            $to='en';
        }elseif ($lang=="zh_cn"){
            $to='zh';
        }
        $fields = $this->request->request("fields");
        $fields = (array)json_decode($fields, true);
        $BaiduTransApi=new BaiduTransService($btConfig['appid'],$btConfig['seckey']);
        foreach ($fields as &$item){
            if($item['value']){
                $item['value']=$BaiduTransApi->translate($item['value'],$from,$to);
                sleep(1);
            }
        }
        $return=[];
        $return['lang']=$lang;
        $return['fields']=$fields;
        $this->success("翻译成功",'',$return);
    }

}
